package com.sfteam.bmw;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import com.sfteam.bmw.api.WeiXinApi;
import com.sfteam.bmw.model.Carousel;
import com.sfteam.bmw.model.Coupon;
import com.sfteam.bmw.model.Feedback;
import com.sfteam.bmw.model.Member;
import com.sfteam.bmw.model.News;
import com.sfteam.bmw.model.Sign;
import com.sfteam.bmw.model.User;
import com.sfteam.bmw.router.ApiRouter;
import com.sfteam.bmw.router.BackendRouter;

public class AppConfig extends JFinalConfig {

	static Log logger = Log.getLog(AppConfig.class);

	@Override
	public void configConstant(Constants me) {
		PropKit.use("bmw_ts.txt");
		me.setDevMode(true);
	}

	@Override
	public void configEngine(Engine me) {
		me.setDevMode(true);
	}

	@Override
	public void configHandler(Handlers me) {
		// Druid监控
		DruidStatViewHandler dvh = new DruidStatViewHandler("/druid",
				new IDruidStatViewAuth() {
					public boolean isPermitted(HttpServletRequest request) {
						return true;
					}
				});
		me.add(dvh);
	}

	@Override
	public void configInterceptor(Interceptors me) {

	}

	@Override
	public void configPlugin(Plugins me) {
		// 数据库连接
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);

		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.addMapping("t_feedback", Feedback.class);// 用户反馈表
		arp.addMapping("t_user", User.class);// 用户表
		arp.addMapping("t_carousel", Carousel.class);// 轮播表
		arp.addMapping("t_coupon", Coupon.class);// 优惠券表
		arp.addMapping("t_member", Member.class);// 会员等级表
		arp.addMapping("t_news", News.class);// 资讯表
		arp.addMapping("t_sign", "openId", Sign.class);// 签到表
		arp.setShowSql(true);
		me.add(arp);

		RedisPlugin redis = new RedisPlugin("bmw", "localhost");
		me.add(redis);
	}

	private DruidPlugin createDruidPlugin() {
		String jdbcUrl = PropKit.get("jdbcUrl");
		String user = PropKit.get("user");
		String password = PropKit.get("password");
		logger.error(jdbcUrl + " " + user + " " + password);
		// 配置druid数据连接池插件
		DruidPlugin dp = new DruidPlugin(jdbcUrl, user, password);
		// 配置druid监控
		dp.addFilter(new StatFilter());
		WallFilter wall = new WallFilter();
		wall.setDbType("mysql");
		dp.addFilter(wall);
		return dp;
	}

	@Override
	public void configRoute(Routes me) {
		me.add("/api/wx", WeiXinApi.class);
		me.add(new ApiRouter());// 前端接口路由
		me.add(new BackendRouter());// 管理后台路由
	}

	/**
	 * 项目启动入口
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		JFinal.start("src/main/webapp", 8081, "/", 5);
	}
}
